package com.dude.screens;


import com.dude.R;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/* the class supplies utility methods to be used in the activities. 
 */
public class ScreensUtility {

	/* this method resizes the listview so that all the elements in the list are shown 
	 * within a scrollableview
	 */
	public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem instanceof ViewGroup) 
            	listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            int num = 1;
            if (listItem.getId()==R.id.comments_row_id)
            {           	
            	TextView tv = (TextView)listItem;
            	String text = (String) tv.getText();
            	double len = (double)text.length();
            	num = (int)Math.ceil(len/30);           	
            }
            if (num<1)
            	num=1;           
            listItem.measure(0, 0);
            totalHeight += (listItem.getMeasuredHeight()*num);
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount()-1));
        listView.setLayoutParams(params);
        listView.requestLayout();            
    }
	
}
